package com.base.cn.platform.os.controller.qa.qatype;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.course.qa.qatype.QaTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 问答类型Controller
 * @author LIZHIPENG
 * @since 2018/4/18
 */
@Controller
@RequestMapping("/admin/qa/qatype")
public class AdminQaTypeController extends AdminBaseController {

    @Autowired
    private QaTypeService qaTypeService;

    /**
     * 查询问答分类列表
     * @param model
     * @return 页面
     */
    @RequestMapping("/queryQaTypeList")
    public String queryQaTypeList(Model model){
        return resultPage("/qa/qatype/qa-type-list");
    }

    /**
     * ajax分页加载问答分类列表
     * @param model
     * @param currentPage 页码
     * @param params 查询条件
     * @return 页面
     */
    @RequestMapping("/ajaxQueryQaTypeList")
    public String ajaxQueryQaTypeList(Model model,
                                      @RequestParam(value = "currentPage", required = false, defaultValue = "1")int currentPage,
                                      @RequestBody Map<String, Object> params){
        Map<String, Object> dataMap = qaTypeService.queryQaTypeListPage(currentPage, params);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/qa/qatype/ajax-qa-type-list");
    }

    /**
     * 查询问答分类列表
     * @return Map<String, Object>
     */
    @RequestMapping("/findQaTypeList")
    @ResponseBody
    public Map<String, Object> findQaTypeList(){
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> qaTypeList = qaTypeService.queryQaTypeList(map);
        return ResultUtil.SUCCESS(qaTypeList);
    }

    /**
     * 去往添加、修改问答分类页面
     * @param model
     * @param qaTypeId 分类ID
     * @return 页面
     */
    @RequestMapping(value = {"/initUpdate","/initAdd"})
    public String initAddOrUpdate(Model model,
                                  @RequestParam(value = "qaTypeId", required = false, defaultValue = "0")BigDecimal qaTypeId){
        Map<String, Object> qaType = null;
        if(qaTypeId.compareTo(new BigDecimal(0)) > 0){
            qaType = qaTypeService.findQaTypeById(qaTypeId);
        }
        if(qaType == null){
            qaType = new HashMap<>();
        }
        model.addAttribute("qaType", qaType);
        return resultPage("/qa/qatype/save-qa-type");
    }

    /**
     * 保存或修改问答分类
     * @return Map<String, Object>
     */
    @RequestMapping("/saveQaType")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "保存或修改问答分类")
    public Map<String, Object> saveQaType(@RequestBody Map<String, Object> qaType){
        return qaTypeService.saveOrUpdateQaType(qaType);
    }

    /**
     * 查看问答分类详情
     * @param model
     * @param qaTypeId
     * @return
     */
    @RequestMapping("/qaTypeInfo")
    public String qaTypeInfo(Model model,
                             @RequestParam("qaTypeId")BigDecimal qaTypeId){
        Map<String, Object> qaType = qaTypeService.findQaTypeById(qaTypeId);
        model.addAttribute("qaType", qaType);
        return resultPage("/qa/qatype/qa-type-info");
    }

    /**
     * 修改问答分类状态
     * @param qaTypeIds 分类ID串
     * @param status 状态：1.正常 2.禁用 3.删除
     * @return
     */
    @RequestMapping("/updateQaTypeStatus")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "修改问答分类状态")
    public Map<String, Object> updateQaTypeStatus(@RequestParam("qaTypeIds")String qaTypeIds,
                                                  @RequestParam("status")Integer status){
        return qaTypeService.updateQaTypeStatus(qaTypeIds, status);
    }
}
